<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #menu {
        position: absolute;
        top: 0;
        left: 0;
        width: 60px;
        height: 60px;
        border: 1px solid red;
      }
      .hidden {
        display: none;
      }
    </style>
  </head>
  <body>
    <ul id="menu" class="hidden">
      <li flag="1">1</li>
      <li flag="2">2</li>
      <li flag="3">3</li>
    </ul>
    <script>
      //鼠标左键菜单事件
      // 1、点击左键 默认属性隐藏，自定义属性显示，自定义内容跟随鼠标移动
      // 2、点击文档时，属性消失
      // 2、委托父元素绑定
      var menu = document.getElementById('menu');
      document.addEventListener('contextmenu', test);
      var timer;
      function test() {
        event.preventDefault();
        menu.classList.remove('hidden');
        menu.style.top = event.clientY + 'px';
        menu.style.left = event.clientX + 'px';
      }
      document.addEventListener('click', function () {
        document.removeEventListener('contextmenu', test);
        menu.classList.add('hidden');
      });
      menu.addEventListener('click', function (event) {
        // document.removeEventListener('click', test);
        var flagName = event.target.getAttribute('flag');
        switch (flagName) {
          case '1':
            console.log('1执行');
          case '2':
            console.log('2执行');
          case '3':
            console.log('3执行');
        }
      });
    </script>
  </body>
</html>
